package com.shujia.flink.sink;

import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.MemorySize;
import org.apache.flink.connector.file.sink.FileSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.time.Duration;

public class Demo01FileSink {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<String> socketDS = env.socketTextStream("master",8888);
        DataStream<String> wordCntDS = socketDS
                .flatMap((line, out) -> {
                    for (String word : line.split(",")) {
                        out.collect(word);
                    }
                }, Types.STRING)
                .map(word -> Tuple2.of(word, 1), Types.TUPLE(Types.STRING, Types.INT))
                .keyBy(kv -> kv.f0, Types.STRING)
                .sum(1)
                .map(kv -> kv.f0 + "," + kv.f1, Types.STRING);

        /*
         * FileSink
         */
        FileSink<String> fileSink = FileSink
                .forRowFormat(new Path("flink/data/output01"), new SimpleStringEncoder<String>("UTF-8"))
                // 设置文件滚动的策略
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                .withRolloverInterval(Duration.ofSeconds(10)) // 多久切换一个文件
                                .withInactivityInterval(Duration.ofSeconds(10)) // 多久没有数据切换
                                .withMaxPartSize(MemorySize.ofMebiBytes(1)) // 多大切换
                                .build())
                .build();

        wordCntDS.sinkTo(fileSink);

        env.execute();


    }
}
